//
//  UserImage.swift
//  learn-ios
//
//  Created by Dong on 2022/4/4.
//

import SwiftUI

struct UseImage: View {
    var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    var body: some View {
        VStack {
            ScrollView {
                Image("gopher1")
                    .resizable() // 设置SwiftUI调整图像大小以适应其空间的模式。
                    .aspectRatio(contentMode: .fit) // 将此视图的尺寸限制为指定的纵横比。
                    .frame(width: 200)

                // 滚动图片
                TabView {
                    Image("gopher1")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 200)
                    Image("gopher2")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 200)
                    Image("gopher1")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 200)
                }
                .tabViewStyle(.page)
                .frame(width: 200, height: 200)

                Spacer()

                // 异步加载图片
                AsyncImage(url: URL(string: "https://img1.baidu.com/it/u=841063360,640381474&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"))

                // 异步加载图片 并设置大小
                AsyncImage(url: URL(string: "https://img1.baidu.com/it/u=841063360,640381474&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500")) { image in
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                } placeholder: {
                    ProgressView()
                }
                .frame(width: 200, height: 200)
            }
            Spacer()
        }
    }
}

struct UseImage_Previews: PreviewProvider {
    static var previews: some View {
        UseImage()
    }
}
